<template>
  <div class="app-container">
    <!-- <upload-excel :on-success="handleSuccess" :before-upload="beforeUpload" /> -->
    <UploadExcel :on-success="handleSuccess" />
    <!-- <el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top:20px;">
      <el-table-column v-for="item of tableHeader" :key="item" :prop="item" :label="item" />
    </el-table> -->
  </div>
</template>

<script>
import { reqImportEmployee } from '@/api/employees'

export default {
  name: 'Import',
  data() {
    return {
    }
  },
  computed: {
    type() {
      return this.$route.query.type
    }
  },
  methods: {
    formatExcelDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1) // 毫秒
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + (month < 10 ? '0' + month : month) + format + (date < 10 ? '0' + date : date)
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    },
    async  handleSuccess({ results, header }) {
      if (this.type === 'user') {
        //   console.log(results, header)
        // 创建新对象映射中文
        const userRelations = {
          '入职日期': 'timeOfEntry',
          '手机号': 'mobile',
          '姓名': 'username',
          '转正日期': 'correctionTime',
          '工号': 'workNumber'
        }
        //   定义一个空数组，接收数据，发送请求时使用
        const arr = []
        // 遍历 results
        results.forEach(item => {
          // 定义一个空对象，用于存储单个员工数据
          const userInfo = {}
          // 遍历item对象
          for (const key in item) {
            if (['timeOfEntry', 'correctionTime'].includes(userRelations[key])) {
              userInfo[userRelations[key]] = this.formatExcelDate(item[key], '-')
            } else {
              // userInfo的key为userRelations的值
              userInfo[userRelations[key]] = item[key]
            }
          }
          arr.push(userInfo)
        })
        console.log(arr)
        await reqImportEmployee(arr)
      }
      this.$router.go(-1)
    }
  }
}
</script>
